package com.aqie.meet.ui.friend;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aqie.libcommon.adapter.CommonAdapter;
import com.aqie.libcommon.adapter.CommonViewHolder;
import com.aqie.libcommon.app.BaseActivity;
import com.aqie.libcommon.app.BaseApplication;
import com.aqie.libcommon.utils.SimpleJsonTool;
import com.aqie.libcommon.view.EmptyView;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.model.db.User;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.meet.R;
import com.aqie.meet.ui.login.AccountActivity;
import com.aqie.meet.view.AImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FriendRequestListActivity extends BaseActivity {

    @BindView(R.id.action_back)
    ImageView mActionBack;
    @BindView(R.id.recycler_view)
    RecyclerView mRecycleView;

    @BindView(R.id.empty_view)
    EmptyView mEmptyView;

    private List<User> mList = new ArrayList<>();
    private CommonAdapter<User> mAdapter;


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_friend_request_list;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        initView();
    }

    private void initView() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mRecycleView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL));
        mAdapter = new CommonAdapter<>(mList, new CommonAdapter.OnBindDataListener<User>() {
            @Override
            public void onBindViewHolder(User model, CommonViewHolder viewHolder, int type, int position) {
                fillData(model, viewHolder);
                // 同意好友请求
                viewHolder.getView(R.id.btn_pass_friend).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // BaseApplication.showToast("添加好友");
                        agreeFriend(model.getUserId(), position);

                    }
                });
                viewHolder.getView(R.id.btn_reject_friend).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // BaseApplication.showToast("添加好友");
                        rejectFriend(model.getUserId(), position);
                    }
                });

                // 切换
                viewHolder.getView(R.id.tag_title).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewHolder.toggle(R.id.setting_about_content);
                    }
                });
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.activity_friend_request_list_item;
            }
        });
        mRecycleView.setAdapter(mAdapter);
        //监听列表滑动
        mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
    }

    private void fillData(User model, CommonViewHolder viewHolder) {
        AImageView mTrueAvatar = viewHolder.getView(R.id.tag_avatar_true);
        mTrueAvatar.setVisibility(View.VISIBLE);
        mTrueAvatar.setImageUrl(model.getAvatar());

        viewHolder.setText(R.id.tag_title, model.getUsername() + " | " + model.getTrade() + " " + model.getPosition());
        viewHolder.setText(R.id.tag_desc, model.getCity() + model.getWorkingYears() + "年工作经验");
        if(TextUtils.isEmpty(model.getCity()) || model.getWorkingYears() == null ){
            viewHolder.goneIfEmpty(R.id.tag_desc);
        }

        viewHolder.setText(R.id.et_resource, model.getAnswerOne());
        viewHolder.setText(R.id.et_need, model.getAnswerTwo());
        viewHolder.setText(R.id.et_know, model.getAnswerThree());
    }

    @Override
    protected void initData() {
        super.initData();
        getFriendRequestList();
    }

    private void getFriendRequestList() {
        RemoteService service = Network.remote();
        Call<RspModel<List<User>>> call = service.friendRequestList();
        call.enqueue(new Callback<RspModel<List<User>>>() {
            @Override
            public void onResponse(Call<RspModel<List<User>>> call, Response<RspModel<List<User>>> response) {
                RspModel<List<User>> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    List<User> talents = body.getData();
                    String rspString = SimpleJsonTool.toJson(talents); Log.e(TAG, rspString);
                    if(talents.size() == 0) {
                        mRecycleView.setVisibility(View.GONE);
                        mEmptyView.setVisibility(View.VISIBLE);
                    } else {
                        mRecycleView.setVisibility(View.VISIBLE);
                        mEmptyView.setVisibility(View.GONE);
                        if (mList.size() > 0) {
                            mList.clear();
                        }
                        mList.addAll(talents);
                        mAdapter.notifyDataSetChanged();
                        Log.e(TAG, "RequestFriendList list:" + mList.toString());
                    }
                }else {
                    Log.e(TAG, "request fail: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<RspModel<List<User>>> call, Throwable t) {

            }
        });
    }

    private void agreeFriend(String friendId, int position) {
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.friendRequestAgree(friendId,null);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {
                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    BaseApplication.showToast("同意成功");
                    getFriendRequestList();
                    mList.remove(position);
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {

            }
        });
    }

    private void rejectFriend(String friendId, int position) {
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.friendRequestRefuse(friendId);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {
                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    BaseApplication.showToast("拒绝成功");
                    getFriendRequestList();
                    mList.remove(position);
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {

            }
        });
    }

    @OnClick(R.id.action_back)
    void back(){
        finish();
    }


}
